package com.cnt.service.impl;

import com.cnt.dao.PersonDao;

import com.cnt.entity.Persons;
import com.cnt.service.PersonService;
import com.cnt.util.FileUtil;
import com.cnt.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonDao personDao;

    @Override
    public ResultVo<List<Persons>> getPersons(int userId) {
        return new ResultVo<>(0, "查询成功", personDao.getPersons(userId));
    }

    @Override
    public ResultVo<Persons> addPerson(Persons person) {
        int result = personDao.insert(person);
        Persons persons = personDao.selectById(result);
        if (persons != null) {
            return new ResultVo<>(0, "新建关联人成功", person);
        }
        return new ResultVo<>(1, "系统异常，新建关联人失败", null);
    }

    @Override
    public ResultVo<Persons> updatePersons(Persons person) {
        int userId = personDao.selectById(person.getPersonId()).getUserId();
        person.setUserId(userId);
        int update = personDao.updateById(person);
        Persons persons = personDao.selectById(person.getPersonId());
        if (update == 1) {
            return new ResultVo<>(0, "上传关联人资料成功", persons);
        }
        return new ResultVo<>(1, "系统异常，上传关联人资料失败", null);
    }

    @Override
    public ResultVo<Persons> deletePersons(int personId) {
        int delete = personDao.deleteById(personId);
        if (delete == 1) {
            return new ResultVo<>(0, "删除成功", null);
        }
        return new ResultVo<>(1, "删除失败", null);
    }

    public InputStream download(Integer personId) {
        Persons persons = personDao.selectById(personId);
        return new ByteArrayInputStream(persons.getFile());
    }

    @Override
    public ResultVo<Persons> getPerson(Integer personId) {
        return new ResultVo<>(0,"查询成功",personDao.selectById(personId));
    }

    @Override
    public ResultVo<String> upload(MultipartFile file, Integer personId) {
        // 检查文件大小
        if (file.getSize() > 16 * 1024 * 1024) { // 文件大小超过16MB
            return new ResultVo<>(2, "文件大于16MB，文件上传失败", null);
        }
        Persons persons = personDao.selectById(personId);
        FileUtil fileUtil = new FileUtil();
        File file1 = null;
        try {

            file1 = fileUtil.convertMultipartFileToFile(file);
            try {
                File file2 = fileUtil.convertToPdf(file1);
                byte[] bytes = fileUtil.addWatermarkToPDF(file2, "捐赠平台专用");
                persons.setFile(bytes);
            } catch (Exception e) {
                e.printStackTrace();
            }
            file1.delete();
        } catch (IOException e) {
            file1.delete();
            e.printStackTrace();
        }
        if (personDao.updateById(persons) == 1) {
            return new ResultVo<>(0, "上传文件成功", "");
        }
        return new ResultVo<>(1, "上传文件失败", "");
    }


}


